package com.lhq.message.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DirectRabbitMqConfig {

    /**
     * description 配置交换机
     * param durable:持久性 autoDelete:自动删除
     * return org.springframework.amqp.core.DirectExchange
     * createTime 2020/9/14 18:45
     **/
    @Bean
    DirectExchange myDirectExchange() {
        // name:交换机的名称
        // durable：交换机有持久(durable)和暂存(transient)两个状态。
        // autoDelete：当所有与之绑定的消息队列都完成了对此交换机的使用后，删掉它。默认是true
        return new DirectExchange("myDirectExchange", true, false);
    }

    /**
     * description 配置队列
     * param durable,autoDeletet,exclusive
     * return org.springframework.amqp.core.Queue
     * createTime 2020/9/14 18:54
     **/
    @Bean
    Queue myDirectQueue() {
        //durable,autoDeletet:
        //exclusive:只被一个连接使用，当连接关闭后，队列即将被删除
        return new Queue("myDirectQueue", true, false, false);
    }

    /**
     * description 绑定 同时配置路由键
     * param []
     * return org.springframework.amqp.core.Binding
     * createTime 2020/9/14 19:03
     **/
    @Bean
    Binding myDirectBinding() {
        // 队列 -> 交换机 -> 路由键
        return BindingBuilder.bind(myDirectQueue()).to(myDirectExchange()).with("myDirectRoutingKey");
    }

}
